/*
    The MIT License

    Copyright (c) 2010 IFMO/GameDev Studio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#pragma once

/*-----------------------------------------------------------------------------
	Physic entity :
-----------------------------------------------------------------------------*/

enum EPhysEntityType {
		PT_STATIC		,
		PT_DYNAMIC		,
		PT_KINEMATIC	,
		PT_TRIGGER		,
	};


class EPhysEntity : public IPhysEntity, public btMotionState {
	public:
							EPhysEntity			( EPhysics *phys );
							~EPhysEntity		( void );

		//	commit entity state :
		virtual void		Spawn				( void );
	
		//	entity state :
		virtual void		SetMesh				( const IPxTriMesh mesh );
		virtual void		SetMass				( float mass );
		virtual float		GetMass				( void ) const;
		virtual void		SetInertia			( const EVector &inertia );
		virtual EVector		GetInertia			( void ) const;
		
		virtual void		GetPose				( EPoint &position, EQuaternion &orient ) const;
		virtual void		SetPose				( const EPoint &position, const EQuaternion &orient );

		//	entity type :		
		virtual void		SetStatic			( void ) { type = PT_STATIC		; };
		virtual void		SetDynamic			( void ) { type = PT_DYNAMIC	; };
		virtual void		SetKinematic		( void ) { type = PT_KINEMATIC	; };
		virtual void		SetTrigger			( void ) { type = PT_TRIGGER	; };

		//	entity dynamic :		
		virtual void		ZeroForces			( void );
		virtual void		AddForce			( const EVector &force );
		virtual void		AddTorque			( const EVector &torque );
		virtual void		AddForceAtPos		( const EVector &force, const EPoint &point );
		virtual void		AddForceAtLocalPos	( const EVector &force, const EPoint &point );
		
		virtual void		GetTotalForces		( EVector &force, EVector &torque ) const;

		virtual EVector		GetLocalPointVelocity	( const EPoint &point ) const;
		virtual EVector		GetCMOffset			( void ) const;

		virtual EVector		GetLinearVelocity	( void ) const;
		virtual void		SetLinearVelocity	( const EVector &velocity );

		virtual EVector		GetAngularVelocity	( void ) const;
		
		//virtual void		CharacterSetup		( float height, float width, float crouch_height, float mass, float gravity_affection );
		//virtual void		CharacterJump		( float initial_velocity );
		//virtual void		CharacterMove		( const EVec4 &direction );

		//	btMotionState stuff :		
		virtual void		getWorldTransform	( btTransform& worldTrans ) const;
		virtual void		setWorldTransform	( const btTransform& worldTrans );
		
		btRigidBody			*GetBody			( void ) { return body; }
		
	protected:
		void				ConstructRigidBody	( void );
		void				ConstructDynamic	( void );
		void				ConstructStatic		( void );
		void				DestructRigidBody	( void );
		
		btCollisionShape	*cookTrimeshShape	( const IPxTriMesh mesh );
		btCollisionShape	*cookConvexHull		( const IPxTriMesh mesh );
		
	
		WeakRef<EPhysics>	phys;
		EPhysEntityType		type;
	
		EQuaternion	orient;
		EPoint		position;
		float		mass;
		IPxTriMesh	mesh;

		btCollisionShape	*shape;	
		btTriangleMesh		*trimesh;
		btRigidBody			*body;
	};


